﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Utils;

public partial class order_view : FBasePage
{
    // Fields

    public Maticsoft.BLL.orderdetail detailbll = new Maticsoft.BLL.orderdetail();
    public List<Maticsoft.Model.orderdetail> detaillist = new List<Maticsoft.Model.orderdetail>();
    public Maticsoft.BLL.Order orderbll = new Maticsoft.BLL.Order();
    public string strCurrency = "";
    public string strOrderCTime = "";
    public string strOrderID = "";
    public string strOrigin = "";
    public string strPrice = "";
    public string strQuantity = "";
    public string strState = "";
    public string strTeamID = "";
    public string strTeamTitle = "";

    // Methods
    public void GetExpress()
    {
    }

    public string GetTeam(string id)
    {
        Maticsoft.Model.Team model = new Maticsoft.Model.Team();
        model = new Maticsoft.BLL.Team().GetModel(Convert.ToInt32(id));
        if (model != null)
        {
            return model.Title;
        }
        return "";
    }

    private void initCard(string cardId)
    {
        StringBuilder builder = new StringBuilder();
        Maticsoft.Model.Card model = new Maticsoft.BLL.Card().GetModel(cardId);
        builder.Append("<tr>");
        builder.Append(" <td class=\"left\">代金券：" + cardId + "</td>");
        builder.Append("<td>" + model.Credit + "</td>");
        builder.Append("<td>x</td>");
        builder.Append("<td>1</td>");
        builder.Append("<td>=</td>");
        builder.Append(string.Concat(new object[] { "<td class=\"total\"><span class=\"money\">", this.strCurrency, "</span>", model.Credit, "</td>" }));
        if (model.consume.ToUpper() == "Y")
        {
            builder.Append("<td class=\"status\">已使用</td>");
        }
        else
        {
            builder.Append("<td class=\"status\">-</td>");
        }
        builder.Append("</tr>");
        this.ltCard.Text = builder.ToString();
    }

    private void initCoupon()
    {
        StringBuilder builder = new StringBuilder();
        builder.Append("<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" class=\"data-table\">");
        builder.Append("<tr>");
        builder.Append(" <th>优惠券：</th>");
        Maticsoft.Model.Team model = new Maticsoft.Model.Team();
        model = new Maticsoft.BLL.Team().GetModel(Convert.ToInt32(this.strTeamID));
        if (Maticsoft.BLL.Team.istuan(Convert.ToInt32(model.Now_number.ToString()), Convert.ToInt32(model.Min_number)))
        {
            DataSet list = new Maticsoft.BLL.Coupon().GetList(" Order_id=" + this.strOrderID);
            builder.Append("<td class=\"other-coupon\">");
            if ((list.Tables.Count > 0) && (list.Tables[0].Rows.Count > 0))
            {
                for (int i = 0; i < list.Tables[0].Rows.Count; i++)
                {
                    DataRow row = list.Tables[0].Rows[i];
                    builder.Append(string.Concat(new object[] { "<p>", row["Id"], " - ", row["Secret"], "</p>" }));
                }
            }
            builder.Append("</td>");
            builder.Append("</tr>");
        }
        else
        {
            builder.Append("<td class=\"other-coupon\">");
            builder.Append(base.ASSystem.Couponname + "将在团购成功后，由系统自动发放</td>");
            builder.Append("</td>");
            builder.Append("</tr>");
        }
        builder.Append("<tr>");
        builder.Append(" <th>使用方法：</th>");
        builder.Append(" <td>至商家消费时，请出示" + base.ASSystem.Couponname + "，配合商家验证券的编号及密码</td>");
        builder.Append("</tr>");
        builder.Append("</table>");
        this.ltCoupon.Text = builder.ToString();
    }

    private void initExpress(int TeamID)
    {
        StringBuilder builder = new StringBuilder();
        Maticsoft.BLL.Order order = new Maticsoft.BLL.Order();
        Maticsoft.Model.Order model = new Maticsoft.Model.Order();
        model = order.GetModel(TeamID);
        builder.Append("<tr>");
        builder.Append("<td class=\"left\">快递</td>");
        builder.Append("<td>" + model.Fare.ToString() + "</td>");
        builder.Append("<td>x</td>");
        builder.Append("<td>1</td>");
        builder.Append("<td>=</td>");
        builder.Append("<td class=\"total\"><span class=\"money\">" + this.strCurrency + "</span>" + model.Fare.ToString() + "</td>");
        builder.Append("<td class=\"status\">-</td>");
        builder.Append("</tr>");
        this.ltExpress.Text = builder.ToString();
    }

    private void initExpressInfo(Maticsoft.Model.Order mOrder)
    {
        DataSet list;
        StringBuilder builder = new StringBuilder();
        builder.Append("<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" class=\"data-table\">");
        builder.Append(" <tr>");
        builder.Append(" <th>快递：</th>");
        builder.Append("");
        if (mOrder.Express_id > 0)
        {
            Maticsoft.BLL.Category category = new Maticsoft.BLL.Category();
            list = category.GetList(" Zone='express' and id=" + mOrder.Express_id);
            if (((list.Tables.Count > 0) && (list.Tables[0].Rows.Count > 0)) && ((list.Tables[0].Rows[0]["name"] != null) && (list.Tables[0].Rows[0]["name"].ToString() != "")))
            {
                builder.Append("<td>" + list.Tables[0].Rows[0]["name"].ToString() + "：" + mOrder.Express_no + "</td>");
            }
        }
        else
        {
            builder.Append(" <td class=\"other-coupon\">请耐心等待发货</td>");
        }
        builder.Append("</tr>");
        if (mOrder.Express_id > 0)
        {
            list = new Maticsoft.BLL.Category().GetList(" Zone='express' and id=" + mOrder.Express_id);
            if (list.Tables[0].Rows.Count > 0)
            {
                builder.Append(Maticsoft.BLL.Order.Getxml(list.Tables[0].Rows[0]["Ename"].ToString(), mOrder.Express_no, "0"));
            }
        }
        builder.Append("<tr>");
        builder.Append(" <th>收件人：</th>");
        builder.Append("<td>" + mOrder.Realname + "</td>");
        builder.Append(" </tr>");
        builder.Append("<tr>");
        builder.Append("<th>收件地址：</th>");
        builder.Append("<td>" + mOrder.Address + "</td>");
        builder.Append("</tr>");
        builder.Append("<tr>");
        builder.Append(" <th>手机号码：</th>");
        builder.Append("<td>" + mOrder.Mobile + "</td>");
        builder.Append("</tr>");
        builder.Append("</table>");
        this.ltExpressInfo.Text = builder.ToString();
    }

    private void initPage()
    {
        if ((base.Request["id"] != null) && (base.Request["id"].ToString() != ""))
        {
            this.strOrderID = base.Request["id"].ToString();
            Maticsoft.Model.Order model = new Maticsoft.BLL.Order().GetModel(int.Parse(this.strOrderID));
            if ((model == null) || (model.User_id != base.ASUser.Id))
            {
                base.Response.Redirect("~/index.aspx");
                base.Response.End();
            }
            else
            {
                this.strOrderCTime = DateTime.Parse(model.Create_time.ToString()).ToString("yyyy-MM-dd HH-mm");
                this.strTeamID = model.Team_id.ToString();
                Maticsoft.Model.Team mTeam = new Maticsoft.BLL.Team().GetModel(int.Parse(this.strTeamID));
                if (model.State == "pay")
                {
                    this.strState = "交易成功";
                }
                if (model.State == "unpay")
                {
                    this.strState = "-";
                }
                if (base.ASSystem != null)
                {
                    this.strCurrency = base.ASSystem.Currency;
                }
                this.strPrice = model.Price.ToString();
                this.strQuantity = model.Quantity.ToString();
                this.strOrigin = (float.Parse(this.strPrice) * float.Parse(this.strQuantity)).ToString();
                if (model.Express == "Y")
                {
                    this.initExpress(model.Id);
                }
                if (model.State == "pay")
                {
                    this.initStateInfo(model);
                }
                if (mTeam != null)
                {
                    this.strTeamTitle = mTeam.Title;
                    if (model.Card_id != "")
                    {
                        this.initCard(model.Card_id);
                    }
                    if (mTeam.Delivery == "coupon")
                    {
                        this.initCoupon();
                    }
                    else if (mTeam.Delivery == "express")
                    {
                        this.initExpressInfo(model);
                    }
                    else if (mTeam.Delivery == "pickup")
                    {
                        this.initPickUp(mTeam);
                    }
                }
                else
                {
                    this.initExpressInfo(model);
                }
            }
        }
    }

    private void initPickUp(Maticsoft.Model.Team mTeam)
    {
        StringBuilder builder = new StringBuilder();
        builder.Append("<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" class=\"data-table\">");
        builder.Append("<tr>");
        builder.Append("<th>自取：</th>");
        builder.Append("<td class=\"other-coupon\"></td>");
        builder.Append("</tr>");
        builder.Append(" <tr>");
        builder.Append("<th>取货地址：</th>");
        builder.Append("<td>" + mTeam.Address + "</td>");
        builder.Append("</tr>");
        builder.Append(" <tr>");
        builder.Append("<th>联系电话：</th>");
        builder.Append("<td>" + mTeam.Mobile + "</td>");
        builder.Append("</tr>");
        builder.Append("</table>");
        this.ltPickUp.Text = builder.ToString();
    }

    private void initStateInfo(Maticsoft.Model.Order mOrder)
    {
        StringBuilder builder = new StringBuilder();
        builder.Append("<tr>");
        builder.Append(" <td class=\"left\"></td>");
        builder.Append("<td></td>");
        builder.Append("<td></td>");
        builder.Append("<td></td>");
        builder.Append("<td></td>");
        builder.Append("<td class=\"total\"><span class=\"money\">" + this.strCurrency + "</span>" + mOrder.Origin.ToString() + "</td>");
        builder.Append("<td class=\"status\">交易成功</td>");
        builder.Append("</tr>");
        this.ltState.Text = builder.ToString();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        this.MyWebside1.Select = "order";
        if (!this.Page.User.Identity.IsAuthenticated)
        {
            base.Response.Redirect("~/account/Login.aspx");
        }
        if (!this.Page.IsPostBack && (base.Request["id"] != null))
        {
            if (Utility.IsNum(base.Request["id"].ToString()))
            {
                this.initPage();
            }
            else
            {
                this.SetError("对不起，参数非法");
            }
        }
    }

}